/**
 * class  Button
 * 按钮组件
 * author haicent
 * email haicent@126.com
 * version 0.01
 */
package as3.component.controls
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	
	import as3.component.core.Dir;
	import as3.component.core.Filters;
	import as3.component.event.SizeChangeEvent;
	import as3.component.geom.IntRectangle;
	import as3.component.support.BaseButton;
	import as3.style.ColorEnums;
	
	public class Button extends BaseButton
	{
		
		//按钮图标
		protected var _icon:DisplayObject;
		
		//按钮显示文本
		public var _textField:Label;
		private var _label:String;
		
		//图标和文本的容器
		protected var _labelIconBox:Sprite;
		
		/**文本额图标局上下左右的间距*/
		protected var _leftPadding:Number = 6;
		protected var _rightPadding:Number = 6;
		protected var _topPadding:Number = 3;
		protected var _bottomPadding:Number = 3;
		
		//最小宽度
		protected var _minWidth:Number = 80;
		//最小高度
		protected var _minHeight:Number = 26;
		
		//图标和文本的间距
		protected var _iconlabelGapValue:Number;
		
		protected var _upTextColor:uint;
		protected var _overTextColor:uint;
		protected var _downTextColor:uint;
		protected var _enableTextColor:uint;
		protected var _fontBold:Boolean;
		protected var _fontSize:uint;
		public var _filterColor:uint;
		protected var _upFilterColor:uint;
		protected var _downFilterColor:uint;
		protected var _overFilterColor:uint;
		protected var _unFilterColor:uint;
		
		private var _upGlowFilter:GlowFilter;
		private var _downGlowFilter:GlowFilter;
		private var _overGlowFilter:GlowFilter;
		private var _unGlowFilter:GlowFilter;
		
		//图标在文本的方位，可选项有left,right,top,bottom
		private var _defaultIconDir:String;
		
//		private const OFFSET_Y:int = 1;/**居中需要往下移动一个像素，由于图片原因*/
		
		public function Button(labelValue:String = "按钮", iconValue:DisplayObject = null, iconlabelGapValue:Number = 0, iconDirValue:String = "")
		{
			_label = labelValue;
			_icon = iconValue;
			_defaultIconDir = iconDirValue;
			_iconlabelGapValue = iconlabelGapValue || 0;
			super();
		}
		
		//设置UI信息
		override protected function initUI():void
		{
			super.initUI();
			_uiLibrary["upImage"] = "ChannelUp";
			_uiLibrary["overImage"] = "ChannelOver";
			_uiLibrary["downImage"] = "ChannelDown";
			_uiLibrary["disabledImage"] = "ChannelUn";
			_upTextColor = _defaultSkin.btnCssTable.upTextColor || 0x000000;
			_overTextColor = _defaultSkin.btnCssTable.overTextColor || 0x000000;
			_downTextColor = _defaultSkin.btnCssTable.downTextColor || 0x000000;
			_enableTextColor = _defaultSkin.btnCssTable.enableTextColor || 0x666666;
			_fontBold = _defaultSkin.btnCssTable.fontBold || false;
			_fontSize = _defaultSkin.btnCssTable.fontSize || 12;
		}
		
		override protected function initCom():void
		{
			super.initCom();
			setMasked(true);
			_upGlowFilter = new GlowFilter(_upFilterColor, 1, 2, 2, 16, 1, false, false);
			_downGlowFilter = new GlowFilter(_downFilterColor, 1, 2, 2, 16, 1, false, false);
			_overGlowFilter = new GlowFilter(_overFilterColor, 1, 2, 2, 16, 1, false, false);
			_unGlowFilter = new GlowFilter(_unFilterColor, 1, 2, 2, 16, 1, false, false);
			_textField = new Label();
			_textField.adaptive = true;
			_textField.multiline = true;
			_textField.color = _upTextColor;
			_textField.htmlText = _label;
			_textField.selectable = false;
			_textField.bold = _fontBold;
			_textField.fontSize = _fontSize;
			_textField.filters = [_upGlowFilter];
//			_textField.y = OFFSET_Y;
			_labelIconBox = new Sprite();
			_labelIconBox.mouseEnabled = false;
			_labelIconBox.mouseChildren = false;
			_labelIconBox.addChild(_textField);
			if (_icon) _labelIconBox.addChild(_icon);
			addChild(_labelIconBox);
		}
		
		override protected function creatUI():void
		{
			super.creatUI();
			contentChange();
		}
		
		override protected function initListener():void
		{
			super.initListener();
			addEventListener(MouseEvent.ROLL_OVER, $onRollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, $onRollOutHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, $onMouseDownHandler);
			addEventListener(MouseEvent.MOUSE_UP, $onMouseUpHandler);
			_textField.addEventListener(SizeChangeEvent.SIZE_CHANGE, $onLabelSizeChangeHandler);
		}
		
		public function get textField():Label
		{
			return _textField;
		}
		
		public function set label(value:String):void
		{
			if (value == _label) return;
			
			_label = value;
			_textField.htmlText = value;
			contentChange();
			reSetLabelPosition();
		}
		
		public function get label():String
		{
			return _textField.htmlText;
		}
		
		public function get text():String
		{
			return _textField.text;
		}
		
		public function set icon(value:DisplayObject):void
		{
			if (_icon == value) return;
			if (_icon)
			{
				if (_labelIconBox.contains(_icon)) 
					_labelIconBox.removeChild(_icon);
			}
			_icon = value;
			contentChange();
		}
		
		public function set iconDir(value:String):void
		{
			if (_defaultIconDir == value) return;
			
			 _defaultIconDir = value;
			 contentChange();
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			reSetLabelPosition();
		}
		
		override public function get width():Number
		{
			return super.width;
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			reSetLabelPosition();
		}
		
		override public function get height():Number
		{
			return super.height;
		}
		
		override public function set enabled(value:Boolean):void
		{
			super.enabled = value;
			if (value)
			{
				if(this.mouseEnabled == false)
				{
//					this.mouseChildren = true;
//					this.mouseEnabled = true;
					_textField.color = _upTextColor;
					_textField.filters = [_upGlowFilter];
				}
				if (_icon) {
					_icon.filters = null;
					_icon.alpha = 1;
				}
			} else {
				if(this.mouseEnabled == true)
				{
//					this.mouseChildren = false;
//					this.mouseEnabled = false;
					_textField.color = _enableTextColor;
					_textField.filters = [_unGlowFilter];
				}
				if (_icon) {
					_icon.filters = [Filters.GRAY_FILTER];
					_icon.alpha = 0.6;
				}
			}
		}
		
		/**
		 *当有文本，图标或者一些可定义的参数修改时调用，重新布局 
		 * 
		 */		
		protected function contentChange():void
		{
			var iconWidth:Number = 0;
			var iconHeight:Number = 0;
			var textWidth:Number = 0;
			var textHeight:Number = 0;
			var offy:Number = 0;
			var offx:Number = 0;
			var gap:Number = _iconlabelGapValue;
			if (_icon)
			{
				if (!_labelIconBox.contains(_icon)) 
					_labelIconBox.addChild(_icon);
				iconWidth = _icon.width;
				iconHeight = _icon.height;
				textWidth = _textField.htmlText == "" ? 0 : _textField.width;
				textHeight = _textField.htmlText == "" ? 0 : _textField.height;
				offy = Math.abs((textHeight - iconHeight) * 0.5);
				offx = Math.abs((textWidth - iconWidth) * 0.5);
				if (_textField.htmlText == "")
				{
					if (_labelIconBox.contains(_textField)) 
						_labelIconBox.removeChild(_textField);
				} else {
					if (!_labelIconBox.contains(_textField)) 
						_labelIconBox.addChild(_textField);
				}
				var _x2:int=0;
				switch(_defaultIconDir)
				{
					case Dir.LEFT:
						_icon.x = 0;
						_icon.y = iconHeight > textHeight ? 0 :  offy;
						_textField.x = iconWidth + gap;
						_textField.y = iconHeight > textHeight ? offy : 0 ;
//						_textField.y += OFFSET_Y;
						_x2 = _textField.x + _textField.width;
						break;
					case Dir.RIGHT:
						_textField.x = 0;
						_textField.y = iconHeight > textHeight ? offy : 0 ;
						_icon.x = textWidth + gap;
						_icon.y = iconHeight > textHeight ? 0 :  offy;
//						_textField.y += OFFSET_Y;
						_x2 = _icon.x + _icon.width;
						break;
					case Dir.TOP:
						_icon.y = 0;
						_icon.x = iconWidth >  textWidth ? 0 : offx;
						_textField.y = iconHeight + gap;
						_textField.x = iconWidth >  textWidth ? offx : 0;
						_x2 = _textField.x + _textField.width;
						break;
					case Dir.BOTTOM:
						_textField.y = 0;
						_textField.x = iconWidth >  textWidth ? offx : 0;
						_icon.y = textHeight + gap;
						_icon.x = iconWidth >  textWidth ? 0 : offx;
						_x2 = _textField.x + _textField.width;
						break;
					default:
						_icon.x = 0;
						_icon.y = iconHeight > textHeight ? 0 :  offy;
						_textField.x = iconWidth + 5;
						_textField.y = iconHeight > textHeight ? offy : 0 ;
						_x2 = _textField.x + _textField.width;
						break;
				}
			}
			if(_textfield2)
			{
				_textfield2.x = _x2;
				_textfield2.y = _textField.y;
			}
			
			if (_width > 0)
			{
				width = _width;
			} else {
				var _w:Number = _labelIconBox.width + _leftPadding + _rightPadding;
				width = _w < _minWidth ? _minWidth : _w;
			}
			
			if (_height > 0)
			{
				height = _height;
			} else {
				var _h:Number = _labelIconBox.height + _topPadding + _bottomPadding;
				height = _h < _minHeight ? _minHeight : _h;
			}
			
			enabled = enabled;
		}
		
		private function reSetLabelPosition():void
		{
			setMaskRect(new IntRectangle(0, 0, simpleButton.width, simpleButton.height));
			_labelIconBox.x = (this.width - _labelIconBox.width) * 0.5;
			_labelIconBox.y = (this.height - _labelIconBox.height) * 0.5;
		}
		
		protected function $onRollOverHandler(evt:MouseEvent):void
		{
			if(enabled == true)
			{
				_textField.color = _overTextColor;
				_textField.filters = [_overGlowFilter];
			}
		}
		
		protected function $onRollOutHandler(evt:MouseEvent):void
		{
			if(enabled == true)
			{
				_textField.color = _upTextColor;
				_textField.filters = [_upGlowFilter];
			}
		}
		
		protected function $onMouseDownHandler(evt:MouseEvent):void
		{
			if(enabled == true)
			{
				_textField.color = _downTextColor;
				_textField.filters = [_downGlowFilter];
			}
		}
		
		protected function $onMouseUpHandler(evt:MouseEvent):void
		{
			if(enabled == true)
			{
				_textField.color = _overTextColor;
				_textField.filters = [_overGlowFilter];
			}
		}
		
		private function $onLabelSizeChangeHandler(evt:SizeChangeEvent):void
		{
			contentChange();
			reSetLabelPosition();
		}
		
		protected var _textfield2:Label;
		public function setLabel2(value:String, color:uint = 0xfdc800):void
		{
			if(!_textfield2)
			{
				_textfield2 = new Label;
				_textfield2.adaptive = true;
				_labelIconBox.addChild(_textfield2);
			}
			
			_textfield2.color = color;
			_textfield2.text = value;
			contentChange();
		}
		
		public function set upFiterColor(value:uint):void
		{
			_upFilterColor = value;
			_upGlowFilter = new GlowFilter(_upFilterColor, 1, 2, 2, 16, 1, false, false);
			_textField.filters = [_upGlowFilter];
		}
	}
}